/**************************************************************************//**
* @file     sys_com.c
* @version  V1.00
* $Revision: 5 $
* $Date: 15/05/18 4:02p $
*
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "sys_ext.h"
#include "com_obj.h"
#include "sys_com.h"

#define PT_NEW_LINE           1      //0-n , 1-\r\n , 2-\n\r

/*******************************************************************************
* @brief  __sys_put_ch
* \param[in] none
* \retval: none
*******************************************************************************/
void __sys_put_ch(char ch)
{
#if PT_NEW_LINE == 0
	com.obj01.fun->cycle_snd_byte(ch);
#elif PT_NEW_LINE == 1
	if (ch != '\n')
	{
		com.obj01.fun->cycle_snd_byte(ch);
	}
	else
	{
		com.obj01.fun->cycle_snd_byte('\r');
		com.obj01.fun->cycle_snd_byte('\n');
	}
#elif PT_NEW_LINE == 2
	if (ch != '\n')
	{
		com.obj01.fun->cycle_snd_byte(ch);
	}
	else
	{
		com.obj01.fun->cycle_snd_byte('\n');
		com.obj01.fun->cycle_snd_byte('\r');
	}
#endif
}

/*******************************************************************************
* @brief  sys_hw_console_output
* \param[in] str : string to print
* \retval: none
*******************************************************************************/
void sys_hw_console_output(const char *str)
{
	/* empty console output */
	while (*str)
	{
		__sys_put_ch(*str++);
	}
}

/*******************************************************************************
* @brief  sys_hw_puts
* \param[in]  str : string to print
* \retval: none
*******************************************************************************/
void sys_hw_puts(const char *str)
{
	sys_hw_console_output(str);
}

/*******************************************************************************
* @brief  sys_hw_printf
* \param[in] fmt: format var like printf
* \retval: none
*******************************************************************************/
void sys_hw_printf(char* fmt, ...)
{
	//notice: ensure buffer length is enough
	char  buf_str[200 + 1];
	va_list   v_args;

	va_start(v_args, fmt);
	(void)vsnprintf((char       *)&buf_str[0],
		(size_t) sizeof(buf_str) - 1,
		(char const *)fmt,
		v_args);
	va_end(v_args);

	sys_hw_console_output(buf_str);
}
